// components/room-card/room-card.js
Component({
  properties: {
    room: {
      type: Object,
      value: {}
    },
    isActive: {
      type: Boolean,
      value: false
    }
  },

  data: {
    allDevicesOn: false
  },

  observers: {
    'room.devices': function(devices) {
      if (devices && devices.length > 0) {
        const allOn = devices.every(device => 
          device.status !== 'offline' && device.powerState === 'on'
        )
        this.setData({ allDevicesOn: allOn })
      }
    }
  },

  methods: {
    // 房间点击事件
    onRoomTap() {
      this.triggerEvent('roomtap', {
        roomId: this.properties.room.id
      })
    },

    // 设备点击事件
    onDeviceTap(e) {
      const deviceId = e.currentTarget.dataset.deviceId
      this.triggerEvent('devicetap', {
        deviceId: deviceId,
        roomId: this.properties.room.id
      })
    },

    // 收藏点击事件
    onFavoriteTap(e) {
      e.stopPropagation()
      const room = this.properties.room
      this.triggerEvent('favorite', {
        roomId: room.id,
        isFavorite: !room.isFavorite
      })
    },

    // 添加设备点击事件
    onAddDeviceTap(e) {
      e.stopPropagation()
      this.triggerEvent('adddevice', {
        roomId: this.properties.room.id
      })
    },

    // 切换所有设备状态
    onToggleAllDevices(e) {
      e.stopPropagation()
      const room = this.properties.room
      const newState = !this.data.allDevicesOn
      
      this.triggerEvent('toggleall', {
        roomId: room.id,
        state: newState
      })

      this.setData({ allDevicesOn: newState })
    },

    // 房间设置点击事件
    onRoomSettingsTap(e) {
      e.stopPropagation()
      this.triggerEvent('settings', {
        roomId: this.properties.room.id
      })
    },

    // 场景点击事件
    onSceneTap(e) {
      e.stopPropagation()
      this.triggerEvent('scene', {
        roomId: this.properties.room.id
      })
    }
  }
})